<?php
namespace Home\Controller;
use Think\Controller;
use Think\Upload;
class InfoController extends BaseController{
	public function __construct(){
		parent::__construct();
		if (!isset($_SESSION['name'])) {
			$url=U("Login/index");
			Header("Location:$url"); 
		}
	}
	
	//载入消息发布页面的表单
	public function index(){
		$catmodel=M("Category");
		$data=$catmodel->where("1")->select();
		$cates=$this->tree($data);
		$this->assign("cates",$cates);
		
		$areamodel=M("Area");
		$areas=$areamodel->where(1)->select();
		
		$this->assign("areas",$areas);
		
		$this->display();
	}
	
	//插入数据
	public function insert(){
		
		if (!check_code($_POST['validate'])){
			$this->error("验证码错误!");
		}
		$data['cat_id']=$_POST['cat_id']+0;
		$catemodel=M("Category");
		$cates=$catemodel->where("parent_id={$data['cat_id']}")->select();
		if (count($cates)>0){
			$this->error("不允许在顶级分类下发布消息!");
			
		}
	
		//$data['user_id']=1;
		if (empty($_POST['title'])) {
			$this->error("消息标题不能为空!");
		}
		if (empty($_POST['area_id'])) {
			$this->error("你没有选择地区!");
		}
		if (empty($_POST['linkman'])) {
			$this->error("你没有填写联系人!");
		}
		if (empty($_POST['adds'])) {
			$this->error("你没有填写联系地址!");
		}
		
		if (empty($_POST['content'])) {
			$this->error("信息内容不能为空!");
		}
		$data['user_id']=$_SESSION['id'];
		$data['info_title']=$_POST['title'];
		$data['area_id']=$_POST['area_id']+0;
		$data['info_linkman']=$_POST['linkman'];
		$data['info_adds']=$_POST['adds'];
		$data['info_price']=$_POST['price']==0?"面议":$_POST['price']+0;
		$data['info_content']=$_POST['content'];
		$data['info_tel']=$_POST['tel']+0;
		$data['info_wechat']=$_POST['wechat'];
		$data['info_qq']=$_POST['qq']+0;
		$userinfomodel=M("Userinfo");
		$user=$userinfomodel->where("user_id={$data['user_id']}")->find();
		if ($user['is_vip']==0) {
			$data['is_varify']=0;
		}else if($user['is_vip']==1){
			$data['is_varify']=1;
		}
		$data['info_createtime']=time();
		$data['info_overtime']=$data['info_createtime']+($_POST['overdate']*24*3600);
		
		//数据库敏感符号转义+html实体转义
		$data=deepspecialchars($data);
		//$fileimg=$_FILES['fileimg'];
		$config = array(   
				'maxSize'    =>512000,
				'savePath'   =>    'info/',
				'saveName'   =>    array('uniqid',''),
				'exts'       =>    array('jpg', 'gif', 'png', 'jpeg','gz'),
				'autoSub'    =>    true,
				'subName'    =>    array('date','Ymd'));
		$upload=new \Think\Upload($config);
		$infos=$upload->upload();
		$arr_flie=array();
		foreach ($infos as $info){
			array_push($arr_flie,$info['savepath'].$info['savename']);
		}
		$data['info_img']=implode($arr_flie, ",");
		
		file_exists("./Uploads/small_img/".date('Ymd'))?"./Uploads/small_img/".date('Ymd'):mkdir("./Uploads/small_img/".date('Ymd'));
		
		 //只生成第一个图片的缩略图$arr_flie[0]
		$info_miniimg="./Uploads/small_img/".date('Ymd')."/".date("YmdHis").rand(100,999).".png";
		 if ($this->img_small($arr_flie[0], 100, 100,  $info_miniimg)){
		 	//从第10个开始截取字符串路径
		 	$data['info_miniimg']=substr($info_miniimg, 10);
		 }
		
		$infomodel=M("Info");
		if ($infomodel->add($data)) {
			$newsmodel=M("News");
			$new['user_id']=$_SESSION['id'];
			if ($user['is_vip']==0) {
				$new['title']="系统消息:消息已经发布,正在审核!";
				$new['content']="由于您是普通用户,您发布的信息:<b>{$data['info_title']}</b>目前处于<b>审核状态</b>,通过审核后会立即显示出来,请稍后
			成为VIP后消息即发即显,快人一步!";
			}else if($user['is_vip']==1){
				$new['title']="系统消息:消息已经发布,正常显示!";
				$new['content']="由于您是我们的VIP用户,您发布的信息:<b>{$data['info_title']}</b>已经用过<b>审核</b>,目前已经正常显示在首页,感谢您对我们的认可!";
			}
			$new['create_time']=time();
			$newsmodel->add($new);
			$this->success("发布成功,请等待审核后,会显示到首页!",U("Index/index"));
		}else {
			$this->error("发布失败!");
		}
		
		
	}
	
	//无线分类的树排序
	public function tree($arr,$pid = 0,$level = 0){
		$res = array();
		foreach ($arr as $v) {
			if ($v['parent_id'] == $pid) {
				//说明找到，保存起来
				//首先，需要将level保存到当前分类中
				$v['level'] = $level;
				//$res[] = $v;
				//继续查找，递归，条件发生变化
				$child = $this->tree($arr,$v['cat_id'],$level+1);
				//返回的过程中，处理，将找到的后代分类，放入到$res中，合并的方式
				$res[] = $v;
				$res = array_merge($res,$child);
			}
		}
		//返回结果
		return $res;
	}
	
	
	//Ajax请求验证分类
	public function getcate(){
		
		$parent_id=$_GET['cat_id'];
		$catemodel=M("Category");
		$cates=$catemodel->where("parent_id={$parent_id}")->select();
		if (count($cates)>0){
			$data['msg']="<font color='red'>不允许在顶级分类下发布消息</font>";
		}else {
			$data['msg']="ok";
		}
		echo json_encode($data);
	}
	
	//验证码
	public function verify(){
		$config =    array(    'fontSize'    =>    14,    // 验证码字体大小
						  'length'      =>    4,     // 验证码位数    
						  'useNoise'    =>    true, // 关闭验证码杂点
						  "expire"	=>60  	//
					);
		$Verify =     new \Think\Verify($config);
		return  $Verify->entry();
	}
	/**
	 * 缩略图
	 * @param string $big_img  //原始大图地址
	 * @param int $width 	//缩略图宽度
	 * @param int $height 	//缩略图高度
	 * @param string $small_img 	//缩略图地址存储路径(文件路径+文件名+.png)
	 */
	private function img_small($big_img, $width, $height, $small_img) {
		//得到原始大图片的一些信息
		$big_img="./Uploads/".$big_img;
		$imgage = getimagesize($big_img); 
		switch ($imgage['2']) { // 图像类型判断
		case 1:
		$im = imagecreatefromgif($big_img);
		break;
		case 2:
		$im = imagecreatefromjpeg($big_img);
		break;
		case 3:
		$im = imagecreatefrompng($big_img);
		break;
		}
		//获取大图片宽度//获取大图片高度
		$img_X = $imgage[0]; 
		$img_Y = $imgage[1]; 
		//创建空白缩略图底板
		$tn = imagecreatetruecolor($width, $height); 
		//复制图像并改变大小
		imagecopyresampled($tn, $im, 0, 0, 0, 0, $width, $height, $img_X, $img_Y);
		if (imagepng($tn, $small_img)){
			return true;
		}else {
			return false;
		}
		
		
		
	}
}